import {
    login,
    getUserAllInfoById,
    modifyUserElseInfo,
    modifyUserBaseInfo, queryUserDeliveredCareersByUserId, getDeliverHistoryById, withdrawDeliverHistory
} from "@/api/app";

import { setToken, removeToken } from "@/utils/auth";

const state = {
    header: {
        disable: true,
        avatar: 'avatars/default.jpg',
        name: '游客',
        token: undefined,
    },
    user_info: {},
    user_all: {},
    user_delivered: []
}

const mutations = {

    SET_APPINFO(state, resp) {
        const { data } = resp.data
        state.user_info = data.user
        state.header.token = data.token
        setToken(data.token)
        if (data.user.name !== null) {
            state.header.name = data.user.name
        } else state.header.name = data.user.username
        state.header.avatar = data.user.user_image
        state.header.disable = false
    },

    LOGOUT(state) {
        state.header = {
            disable: true,
            avatar: 'avatars/default.jpg',
            name: '游客',
            token: undefined,
        }
        state.user_info = {}
        state.user_all = {}
        removeToken()
    },

    SET_USERALL(state, resp) {
        state.user_all = resp
        state.header.avatar = resp.user_image
        if (resp.name != null) {
            state.header.name = resp.name
        } else {
            state.header.name = resp.username
        }
    },

    SET_USER_DELIVERED(state, data) {
        state.user_delivered = data
    }
}

const actions = {

    login({commit}, data) {
        return new Promise((resolve, reject) => {
            login(data).then((resp) => {
                if (resp.data.code === 1) {
                    commit("SET_APPINFO", resp)
                    resolve(true)
                }
                const { msg } = resp.data
                reject(msg)
            }).catch(error => reject(error.msg))
        })
    },

    logout({commit}) {
       commit("LOGOUT")
    },

    getUserAllInfoById({commit}, id) {
        return new Promise((resolve, reject) => {
            getUserAllInfoById(id).then((resp) => {
                commit("SET_USERALL", resp.data.data)
                resolve(true)
            }).catch(error => reject(error.msg))
        })
    },

    modifyUserBaseInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyUserBaseInfo(data.id, data.data).then((resp) => {
                if (resp) resolve(resp)
            }).catch((error) => reject(error))
        })
    },

    modifyUserElseInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyUserElseInfo(data.id, data.data).then((resp) => {
                if (resp) resolve(resp)
            }).catch((error) => reject(error))
        })
    },

    queryUserDeliveredCareersByUserId({commit} ,id) {
        return new Promise((resolve, reject) => {
            queryUserDeliveredCareersByUserId(id)
                .then((resp) => {
                    if (resp.data.code === 1) {
                        commit("SET_USER_DELIVERED", resp.data.data)
                        resolve(true)
                    }
                    reject("error")
                }).catch((error) => reject(error))
        })
    },

    getDeliverHistoryById({commit} ,id) {
        return new Promise((resolve, reject) => {
            getDeliverHistoryById(id)
                .then((resp) => {
                    if (resp.data.code === 1) {
                        resolve(resp.data.data)
                    }
                    reject("error")
                }).catch((error) => reject(error))
        })
    },

    withdrawDeliverHistory({commit} ,id) {
        return new Promise((resolve, reject) => {
            withdrawDeliverHistory(id)
                .then((resp) => {
                    if (resp.data.code === 1) {
                        resolve(true)
                    }
                    reject("error")
                }).catch((error) => reject(error))
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
